﻿using MAdesantis;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestForDevelopers
{
    public partial class frmDevelopers : Form
    {
        public frmDevelopers()
        {
            InitializeComponent();
        }

        private void btoCheck_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtDireccionURL.Text))
            {
                //Uri uri = new Uri(txtDireccionURL.Text);

                String[] str = txtDireccionURL.Text.Split(new Char[] { '/' });
                String fileName = str[str.Length - 1];

                //string st = System.IO.Path.GetFileName(uri.LocalPath);

                MessageBox.Show(fileName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite("http://spserver/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        site.AllowUnsafeUpdates = true;
                        web.AllowUnsafeUpdates = true;

                        SPFile file = null;
                        string nameFile = Util.GetFileNameAndExt("http://spserver/Activities%20of%20Test/000.png");
                        SPFileCollection files = web.Lists["Activities of Test"].RootFolder.Files;

                        foreach (SPFile item in files)
                        {
                            if (item.Name == nameFile)
                            {
                                file = item;
                            }
                        }

                        SPListItem _item = file.Item;


                        UpdatePermissions(_item, web, "spuserfullread;spinstall");


                        web.AllowUnsafeUpdates = false;
                        site.AllowUnsafeUpdates = false;
                    }
                }

            });
        }

        public void UpdatePermissions(SPListItem itemCurrent, SPWeb webCurrent, string Usuarios)
        {
            try
            {
                String[] User = Usuarios.Split(new char[] { ';' });

                foreach (var item in User)
                {

                    itemCurrent.BreakRoleInheritance(true);

                    SPRoleDefinition roleDefinition = webCurrent.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    SPUser spUser = webCurrent.EnsureUser(item);
                    SPRoleAssignment roleAssigment = new SPRoleAssignment(spUser);

                    roleAssigment.RoleDefinitionBindings.Add(roleDefinition);
                    itemCurrent.RoleAssignments.Add(roleAssigment);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
